package com.koron.standard.rating.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.rating.bean.dto.RatingDTO;
import com.koron.standard.rating.bean.dto.RatingExcel;
import com.koron.standard.rating.bean.query.RatingQuery;
import com.koron.standard.rating.bean.vo.RatingVO;
import com.koron.standard.rating.service.RatingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 评级标准(Rating)表控制层
 *
 * @author jzp
 * @since 2022-01-04
 */
@Api(tags = "评级标准接口")
@RestController
@RequestMapping("rating")
public class RatingController {

    @Autowired
    private RatingService ratingService;

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<Page<RatingVO>> page(PageQuery<RatingVO> pageQuery, RatingQuery query) {
        Page<RatingVO> page = ratingService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<RatingDTO> select(@RequestParam String id) {
        RatingDTO ratingDTO = ratingService.getDtoById(id);
        return Response.ok(ratingDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody RatingDTO ratingDTO) {
        ratingService.saveDto(ratingDTO);
        return Response.ok(ratingDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody RatingDTO ratingDTO) {
        ratingService.updateDtoById(ratingDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        ratingService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        ratingService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        ratingService.updateToPublic(ids);
        return Response.ok();
    }


    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/rating_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=设备评级标准.xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), "评级标准");
        // 默认第一行为标题行，Map中的key为标题，value为标题对应的单元格值
        // 设备分类ID 	设备分类名称	一类标准	二类标准	三类标准	四类标准
        List<Map<String,Object>> readAll = reader.readAll();
        ratingService.importFile(readAll);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, RatingQuery query) {
        String path = "excel/rating_standard_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("评级标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<RatingExcel> ratingExcelList = ratingService.listExcel(query);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .sheet("评级标准")
                .doFill(ratingExcelList);
    }

}
